package com.peng.redis;

import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.peng.utils.JacksonUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

/**
 * JedisSample1
 *
 * @author lupeng
 * Created on 2022-04-17
 */
public class JedisWithStringSample {

    private static final Logger logger = LoggerFactory.getLogger(JedisWithStringSample.class);

    private JedisPool pool;

    @BeforeEach
    public void init() {
        pool = new JedisPool("localhost", 6379);
        logger.info("JedisPool create success");
    }

    @AfterEach
    public void destroy() {
        if (pool != null) {
            pool.close();
        }
    }

    @Test
    public void setTest() {
        try (Jedis jedis = pool.getResource()) {
            String key = "key1";
            String res = jedis.set(key, "val1");
            logger.info(res); // 设置成功返回OK
            logger.info(jedis.get(key));
        }
    }

    @Test
    public void setnxTest() {
        try (Jedis jedis = pool.getResource()) {
            String key = "key12";
            long res = jedis.setnx(key, "val111");
            logger.info("res {}", res);
            // 设置成功返回1，失败返回0
        }
    }

    @Test
    public void setWithSetParams() {
        try (Jedis jedis = pool.getResource()) {
            String key = "key1";
            //SetParams.setParams().px(111).xx().nx()
            String res = jedis.set(key, "val23", SetParams.setParams().ex(100));
            logger.info("res {}", res);
            long ttl = jedis.ttl(key);
            logger.info("ttl {}", ttl);
        }
    }

    @Test
    public void getsetTest() {
        try (Jedis jedis = pool.getResource()) {
            String key = "key1";
            String oldVal = jedis.getSet(key, "newvalbygetset1111111");
            logger.info("oldVal {}", oldVal);
        }
    }

    @Test
    public void strlenTest() {
        try (Jedis jedis = pool.getResource()) {
            String key = "key1";
            String res = jedis.set(key, "123456789");
            logger.info("res {}", res);
            long len = jedis.strlen(key);
            logger.info("len {}", len);
        }
    }

    @Test
    public void appendTest() {
        try (Jedis jedis = pool.getResource()) {
            String key = "key1";
            long appendedLength = jedis.append(key, "appendval");
            logger.info("res {}", appendedLength); // 追加之后的字符长度
            logger.info("value {}", jedis.get(key));
        }
    }

    @Test
    public void setrangeTest() {
        try (Jedis jedis = pool.getResource()) {
            String key = "key11";
            jedis.set(key, "hello");
            long res = jedis.setrange(key, 5, "redis");
            logger.info("res {}", res); // 设置之后的字符串长度
            logger.info("value {}", jedis.get(key));

            jedis.setrange("sdfsafsadf", 20, "out");
            printVal("sdfsafsadf");
            // 控制台会将空白字符隐藏
        }
    }

    private void printVal(String key) {
        try (Jedis jedis = pool.getResource()) {
            logger.info("value {}", jedis.get(key));
            logger.info("length {}", jedis.strlen(key));
        }
    }


    @Test
    public void getrangeTest() {
        try (Jedis jedis = pool.getResource()) {
            String key = "keygetrange";
            jedis.set(key, "123456789", SetParams.setParams().ex(100));
            String getrange = jedis.getrange(key, 0, -1);
            logger.info("getrange {}", getrange);
        }
    }

    @Test
    public void incrTest() {
        try (Jedis jedis = pool.getResource()) {
            String key = "keyincr";
            jedis.set(key, "1", SetParams.setParams().ex(100));
            for (int i = 0; i < 100; i++) {
                jedis.incr(key);
            }
            printVal(key);
            for (int i = 0; i < 100; i++) {
                jedis.incrBy(key, i);
            }
            printVal(key);
            for (int i = 0; i < 100; i++) {
                jedis.incrBy(key, -i);
            }
            printVal(key);
            for (int i = 0; i < 100; i++) {
                jedis.decrBy(key, -i);
            }
            printVal(key);
        }
    }

    @Test
    public void msetTest() {
        try (Jedis jedis = pool.getResource()) {
            jedis.mset("key1", "val1", "key2", "val2");
            List<String> mget = jedis.mget("key1", "key2");
            logger.info("mget {}", JacksonUtil.toJson(mget));
        }
    }
}
